<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true"></tree-tools>

        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <tree-tools
            @addDepts="addDepts"
            @editDepts="editDepts"
            @delDepts="getDepartments"
            slot-scope="{ data }"
            :tree-node="data"
          ></tree-tools>
        </el-tree>
      </el-card>

      <!-- 对话框添加  组织架构 -->
      <add-dept
        ref="addDept"
        @editDepts="getDepartments"
        :showDialog.sync="showDialog"
        :tree-node="node"
      ></add-dept>
    </div>
  </div>
</template>

<script>
import { getDepartmentsApi } from "@/api/departments.js";
import treeTools from "./components/tree-tools";
import { tranListToTreeData } from "@/utils";
import addDept from "./components/add-dept";
export default {
  data() {
    return {
      departs: [
        {
          name: "总裁办",
          manager: "曹操",
          children: [{ name: "董事会", manager: "曹丕" }]
        },
        { name: "行政部", manager: "刘备" },
        { name: "人事部", manager: "孙权" }
      ],
      defaultProps: {
        label: "name" // 表示 从这个属性显示内容
      },
      company: {},
      showDialog: false, // 显示窗体
      node: null,
      loading: false // 用来控制进度弹层的显示和隐藏
    };
  },
  components: {
    treeTools,
    addDept
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      this.loading = true;
      const result = await getDepartmentsApi();
      // console.log(result.data.depts, 445566);
      this.company = {
        name: result.data.companyName,
        manager: "负责人",
        id: ""
      };
      //this.departs = result.data.depts; // 需要将其转化成树形结构
      this.departs = tranListToTreeData(result.data.depts, ""); // 需要将其转化成树形结构
      //console.log(this.departs, 4455555);
      this.loading = false;
    },
    //  添加
    addDepts(node) {
      //console.log(node, 456);
      this.showDialog = true;
      this.node = node;
      //console.log(this, 7777);
    },
    //  编辑
    editDepts(node) {
      this.showDialog = true; // 显示新增组件弹层
      this.node = node; // 存储传递过来的node数据
      // 我们需要在这个位置 调用子组件的方法
      // 父组件 调用子组件的方法
      this.$refs.addDept.getDepartDetail(node.id); // 直接调用子组件中的方法 传入一个id
    }
  }
};
</script>
<style lang="scss" scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
